package com.zjw.zy.coreui.view

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.constant.SpecialDay
import com.zjw.zy.coreui.dialog.TwoListDialog
import com.zjw.zy.smi.JustWithIt2
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/13 11:01
 *
 * 日历自定义View
 *
 */
class CalendarView : BaseViewGroup {

    companion object {
        private const val TAG = "CalendarView"
    }

    private val mTwoListDialog: TwoListDialog<String, String> by lazy {
        TwoListDialog.build<String, String>(context).apply {
            //1950~2200年
            val years = ArrayList<String>().apply {
                for (i in 1950..2200) {
                    this.add(i.toString())
                }
            }

            val months = ArrayList<String>().apply {
                for (i in 1..12) {
                    this.add(i.toString())
                }
            }
            this.bindData(years, years, months, months)
            this.bindNegativeCallback { obj1, obj2 ->
                dismiss()
            }.bindPositiveCallback { obj1, obj2 ->
                bindData(obj1.toInt(), obj2.toInt())
                dismiss()
            }.title("请选择日期")
        }
    }

    private var mCurrentYear: Int = 1
    private var mCurrentMonth: Int = 1
    private val mDay: List<CalendarItemView> by lazy {
        ArrayList<CalendarItemView>().apply {
            this.add(findViewById(R.id.calendar_day_row_1_1))
            this.add(findViewById(R.id.calendar_day_row_1_2))
            this.add(findViewById(R.id.calendar_day_row_1_3))
            this.add(findViewById(R.id.calendar_day_row_1_4))
            this.add(findViewById(R.id.calendar_day_row_1_5))
            this.add(findViewById(R.id.calendar_day_row_1_6))
            this.add(findViewById(R.id.calendar_day_row_1_7))
            this.add(findViewById(R.id.calendar_day_row_2_1))
            this.add(findViewById(R.id.calendar_day_row_2_2))
            this.add(findViewById(R.id.calendar_day_row_2_3))
            this.add(findViewById(R.id.calendar_day_row_2_4))
            this.add(findViewById(R.id.calendar_day_row_2_5))
            this.add(findViewById(R.id.calendar_day_row_2_6))
            this.add(findViewById(R.id.calendar_day_row_2_7))
            this.add(findViewById(R.id.calendar_day_row_3_1))
            this.add(findViewById(R.id.calendar_day_row_3_2))
            this.add(findViewById(R.id.calendar_day_row_3_3))
            this.add(findViewById(R.id.calendar_day_row_3_4))
            this.add(findViewById(R.id.calendar_day_row_3_5))
            this.add(findViewById(R.id.calendar_day_row_3_6))
            this.add(findViewById(R.id.calendar_day_row_3_7))
            this.add(findViewById(R.id.calendar_day_row_4_1))
            this.add(findViewById(R.id.calendar_day_row_4_2))
            this.add(findViewById(R.id.calendar_day_row_4_3))
            this.add(findViewById(R.id.calendar_day_row_4_4))
            this.add(findViewById(R.id.calendar_day_row_4_5))
            this.add(findViewById(R.id.calendar_day_row_4_6))
            this.add(findViewById(R.id.calendar_day_row_4_7))
            this.add(findViewById(R.id.calendar_day_row_5_1))
            this.add(findViewById(R.id.calendar_day_row_5_2))
            this.add(findViewById(R.id.calendar_day_row_5_3))
            this.add(findViewById(R.id.calendar_day_row_5_4))
            this.add(findViewById(R.id.calendar_day_row_5_5))
            this.add(findViewById(R.id.calendar_day_row_5_6))
            this.add(findViewById(R.id.calendar_day_row_5_7))
            this.add(findViewById(R.id.calendar_day_row_6_1))
            this.add(findViewById(R.id.calendar_day_row_6_2))
            this.add(findViewById(R.id.calendar_day_row_6_3))
            this.add(findViewById(R.id.calendar_day_row_6_4))
            this.add(findViewById(R.id.calendar_day_row_6_5))
            this.add(findViewById(R.id.calendar_day_row_6_6))
            this.add(findViewById(R.id.calendar_day_row_6_7))
        }
    }

    private val mYearMonth: TextView by lazy { findViewById(R.id.calendar_year_month) }

    private val mLastRow: LinearLayout by lazy { findViewById(R.id.calendar_day_row_6) }
    private val mLastMonth: ImageView by lazy { findViewById(R.id.calendar_last_month) }
    private val mNextMonth: ImageView by lazy { findViewById(R.id.calendar_next_month) }

    private var mListener: Listener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.calendar_view
    }

    override fun onBindListener() {
        mYearMonth.setOnClickListener {
            mTwoListDialog.select1Value(mCurrentYear.toString())
                .select2Value(mCurrentMonth.toString())
            mTwoListDialog.show()
        }

        mLastMonth.setOnClickListener {
            val bean: TimeUtils.TimeBean = TimeUtils.createTimeBean(-1, mCurrentYear, mCurrentMonth)
            bindData(bean.year, bean.month)
        }

        mNextMonth.setOnClickListener {
            val bean: TimeUtils.TimeBean = TimeUtils.createTimeBean(1, mCurrentYear, mCurrentMonth)
            bindData(bean.year, bean.month)
        }

        for (itemView in mDay) {
            itemView.setOnClickListener {
                mListener?.onItemClick(it as CalendarItemView)
                for (view in mDay) {
                    view.isSelected = view == it
                }
            }
        }

        this.setOnClickListener {
            for (view in mDay) {
                view.isSelected = false
            }
        }
    }

    override fun onBindValue() {
        bindData()
    }

    override fun onFindView(rootView: View) {}

    /**
     * 绑定日期，控制在一个月内哈！
     * */
    fun bindData(
        year: Int = TimeUtils.currentTime().year,
        month: Int = TimeUtils.currentTime().month,
    ) {

        val data: List<TimeUtils.TimeBean> = TimeUtils.getTimeBeans(year, month)
        val startWeek = data[0].dayOfWeek

        mCurrentYear = year
        mCurrentMonth = month
        mYearMonth.text =
            ResourcesUtils.getString(R.string.calendar_year_month_text, year, month)

        //隐藏前置日期
        for (i in 0 until startWeek) {
            mDay[i].visibility = View.INVISIBLE
        }

        for (i in data.indices) {
            mDay[startWeek + i].let {
                val timeBean = data[i]
                it.bindData(timeBean)
                it.visibility = View.VISIBLE
                //标记今天
                if (timeBean.eqOfDay(TimeUtils.currentTime())) {
                    it.setBackgroundColor(ResourcesUtils.getColor(R.color.calendar_today_color))
                } else {
                    it.setBackgroundColor(ResourcesUtils.getColor(R.color.transparent))
                    it.isSelected = false
                }
            }
        }

        //隐藏后置日期
        for (i in startWeek + data.size until mDay.size) {
            mDay[i].visibility = View.INVISIBLE
        }
    }

    fun bindListener(listener: Listener) {
        mListener = listener
    }

    /**
     * 选中日期，
     * */
    fun setSelectDay(day: Int) {
        Log.d(TAG, "setSelectDay() called with: day = $day")
        if (day < 1 || day >= 31) {
            return
        }

        for (itemView in mDay) {
            if (itemView.isVisible && itemView.getData().day == day) {
                itemView.isSelected = true
                return
            }
        }
    }

    interface Listener {
        fun onItemClick(view: CalendarItemView)
    }
}